/*
 * wakabatasksendpost.h
 *
 *  Created on: Sep 24, 2011
 *      Author: anonovermind <anonovermind@gmail.com>
 */

#ifndef WAKABATASKSENDPOST_H_
#define WAKABATASKSENDPOST_H_

#include "tasks/tasksendpost.h"
#include "wakabaagent.h"
#include "requestmanager.h"
#include "captchafetchers/captchafetcher.h"

namespace Wishmaster
{
	class WakabaTaskSendPost : public TaskSendPost
	{
		Q_OBJECT
	public:
		WakabaTaskSendPost(const PostDescriptor& post, const QString& captcha,
				Wishmaster2::Network::RequestManager* requestManager, const QString& msg, WakabaAgent* agent) :
					TaskSendPost(post, captcha, requestManager, msg),
					m_post(post),
					m_captcha(captcha),
					m_agent(agent)
		{

		}

	protected:
		virtual void onRequestDone()
		{

		}

		virtual Wishmaster2::Network::RequestPtr makeRequest(const PostDescriptor& post, const QString& captcha,
						Wishmaster2::Network::RequestManager* requestManager)
		{
			QString wakabascript = m_agent->property("wakabascript").toString();
			wakabascript.replace("%BOARD%", post.board);

			Wishmaster2::Network::Url url(std::string("http://") + post.parent_thread.chanName().toStdString() + wakabascript.toStdString());

			Wishmaster2::Network::RequestPtr request = requestManager->createRequest(url);
			request->setParameter("http:method", "POST");
			request->setParameter("task", "post");
			if(post.parent_thread.threadNumericId())
				request->setParameter("parent", post.parent_thread.threadStringId().toStdString());
			request->setParameter("name", "");
			request->setParameter(m_agent->property("field1name").toString().toStdString(), std::string(post.name.toUtf8().constData()));
			request->setParameter("link", "");
			request->setParameter(m_agent->property("field2name").toString().toStdString(), std::string(post.email.toUtf8().constData()));
			request->setParameter(m_agent->property("field3name").toString().toStdString(), std::string(post.topic.toUtf8().constData()));
			request->setParameter(m_agent->property("field4name").toString().toStdString(), std::string(post.text.toUtf8().constData()));
			m_agent->addAttachments(request, post.attachments.count() > 0 ? post.attachments.at(0) : AttachmentDescriptor(), "file");

			if(m_agent->captchaFetcher() && (m_agent->captchaFetcher()->id() == "generic"))
				request->setParameter("captcha", captcha.toStdString());
			request->setParameter("postredir", "1");
			request->setParameter("password", std::string(post.password.toUtf8().constData()));

			if(!captcha.isEmpty())
				m_agent->captchaFetcher()->processRequest(post, captcha, request);

			QString referer;
			if(post.parent_thread.threadNumericId())
				referer = QString("http://%1%2res/%3.html").arg(post.parent_thread.chanName()).arg(post.board).arg(post.parent_thread.threadNumericId());
			else
				referer = QString("http://%1%2").arg(post.parent_thread.chanName()).arg(post.board);
			request->setParameter("http:referer", referer.toStdString());
			return request;
		}

	private:
		PostDescriptor m_post;
		QString m_captcha;
		WakabaAgent* m_agent;
	};
}


#endif /* WAKABATASKSENDPOST_H_ */
